package cn.sinata.wanglin;
import android.content.Context;
import android.graphics.Color;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

import com.baidu.mapapi.SDKInitializer;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.easeui.controller.EaseUI;
import com.hyphenate.easeui.domain.EaseUser;
import com.qihoo.appstore.common.updatesdk.lib.UpdateHelper;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.xilada.xldutils.BaseApplication;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import cn.sinata.wanglin.aliyunoss.OssMyUtils;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.db.dao.GroupDao;
import cn.sinata.wanglin.db.dao.GroupUserDao;
import cn.sinata.wanglin.db.dao.UserDao;
import cn.sinata.wanglin.db.model.User;
import cn.sinata.wanglin.model.Group;

/**
 * description：
 * Created by kaihongwu on 2016/12/05 下午 1:02
 */
public class MyApplication extends BaseApplication {
    public static String houseid=null;//全局选择的当前小区
    public static String houseName=null;//全局选择的当前小区
    public static String city="成都市";//全局地位的当前城市默认
    public static String cityCode="75";//全局地位的当前城市CODE
    public static double lat=30.59251;//测试数据104.061649,30.59251
    public static double lng=104.061649;//测试数据
    public static int circleUnRead = 0;//圈子未读数。

    @Override
    protected String setSharedPreferencesName() {
        return "wanglin";
    }
    @Override
    public void onCreate() {
        super.onCreate();
        EMOptions options = new EMOptions();
        // 默认添加好友时，是不需要验证的，改成需要验证
        options.setAcceptInvitationAlways(false);
        options.setAutoLogin(true);
        options.setUseHttps(true);
        options.setDeleteMessagesAsExitGroup(true);
        EaseUI.getInstance().init(this,options);
        EaseUI.getInstance().setUserProfileProvider(new EaseUI.EaseUserProfileProvider() {
            @Override
            public EaseUser getUser(EMMessage.ChatType chatType,String groupId, String username) {
                EaseUser easeUser = new EaseUser(username);
                User user;
                if (chatType == EMMessage.ChatType.GroupChat) {
                    user = GroupUserDao.getInstance(MyApplication.this).getUser(groupId,username);
                } else {
                    user = UserDao.getInstance(MyApplication.this).getUser(username);
                }

                if (user != null) {
                    //当前登录用户，获取本地存储的头像信息,以保证是最新。
                    String headUrl = user.getHeadUrl();
                    if (TextUtils.equals(username,EMClient.getInstance().getCurrentUser())) {
                        headUrl = SharedPreferencesUtils.getString(Constant.SP_HEADURL);
                    }
                    easeUser.setAvatar(headUrl);
                    easeUser.setNickname(user.getName());
                    easeUser.setNick(user.getName());
                }

                return easeUser;
            }
        });

        EaseUI.getInstance().setGroupProvider(new EaseUI.EaseGroupProfileProvider() {
            @Override
            public String getGroupName(String groupId) {
                String userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
                Group group = GroupDao.getInstance(MyApplication.this).getGroup(userId,groupId);
                if (group == null) {
                    return null;
                }
                return group.getGroupName();
            }
        });

        EaseUI.getInstance().setSettingsProvider(new EaseUI.EaseSettingsProvider() {
            @Override
            public boolean isMsgNotifyAllowed(EMMessage message) {
                if (message.getChatType() == EMMessage.ChatType.GroupChat) {
                    String userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
                    Group group = GroupDao.getInstance(MyApplication.this)
                            .getGroup(userId,message.getTo());
                    return group.getMsgStatus() != 1;
                }

                return SharedPreferencesUtils.getBoolean(Constant.SP_NOTIFY_ALLOWED,true);
            }

            @Override
            public boolean isMsgSoundAllowed(EMMessage message) {

                if (message.getChatType() == EMMessage.ChatType.GroupChat) {
                    String userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
                    Group group = GroupDao.getInstance(MyApplication.this)
                            .getGroup(userId,message.getTo());
                    return group.getMsgStatus() != 1;
                }

                return SharedPreferencesUtils.getBoolean(Constant.SP_VOICE,true);

            }

            @Override
            public boolean isMsgVibrateAllowed(EMMessage message) {
                if (message.getChatType() == EMMessage.ChatType.GroupChat) {
                    String userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
                    Group group = GroupDao.getInstance(MyApplication.this)
                            .getGroup(userId,message.getTo());
                    return group.getMsgStatus() != 1;
                }
                return SharedPreferencesUtils.getBoolean(Constant.SP_SHOCK,true);
            }

            @Override
            public boolean isSpeakerOpened() {
                return true;
            }
        });

//        EMClient.getInstance().setDebugMode(true);
        //打开Log开关 正式发布时请关闭
//        RedPacket.getInstance().setDebugMode(true);

        OssMyUtils.init(this);
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        SDKInitializer.initialize(getApplicationContext());

        PlatformConfig.setWeixin("wx52b352dbd1948e3c", "1bd6d247ed1bb53bcdccf962767b2ec0");
        PlatformConfig.setSinaWeibo("3438384404", "c369fe8835412153ee2344562accc3e1");
        PlatformConfig.setQQZone("1105861416", "3kTEOHjj99hiKUGL");
        UMShareAPI.get(this);
        UpdateHelper.getInstance().setDebugMode(true);
        UpdateHelper.getInstance().init(getApplicationContext(), Color.parseColor("#fec81e"));
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

}
